package com.ccrfid.rmc6.service.business.visitroom;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.visitroom.RoomVisitRuleEntity;
import com.ccrfid.rmc6.repository.business.visitroom.RoomVisitRuleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.List;

@Service
@Transactional
public class RoomVisitRuleServiceImpl implements RoomVisitRuleService {
    private static final Logger logger = LoggerFactory.getLogger(RoomVisitRuleServiceImpl.class);
    @Autowired
    private RoomVisitRuleRepository roomVisitRuleRepository;

    @Override
    public List<RoomVisitRuleEntity> getAllVisitRules() {
        return roomVisitRuleRepository.getAllBy();
    }

    @Override
    public RoomVisitRuleEntity getVisitRule(Long ruleId) {
        return roomVisitRuleRepository.getById(ruleId);
    }

    @Override
    public RoomVisitRuleEntity updateVisitRule(Long ruleId, RoomVisitRuleEntity entity) throws AppException {
        if (!roomVisitRuleRepository.existsById(ruleId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (!ruleId.equals(entity.getId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.different_args.getCode(), CustomErrors.different_args.getReason(), "");
        }
        return roomVisitRuleRepository.save(entity);
    }
}
